package com.atguigu.gulimall.product.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.entity.BrandEntity;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.BrandService;
import com.atguigu.gulimall.product.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.fasterxml.jackson.databind.annotation.JsonAppend;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.atguigu.gulimall.product.dao.CategoryBrandRelationDao;
import com.atguigu.gulimall.product.entity.CategoryBrandRelationEntity;
import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.util.CollectionUtils;


@Service("categoryBrandRelationService")
@Slf4j
public class CategoryBrandRelationServiceImpl extends ServiceImpl<CategoryBrandRelationDao, CategoryBrandRelationEntity> implements CategoryBrandRelationService {

    @Autowired
    private CategoryService categoryService;

    @Autowired
    private BrandService brandService;

    @Autowired
    private CategoryBrandRelationService catBrandRelation;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryBrandRelationEntity> page = this.page(
                new Query<CategoryBrandRelationEntity>().getPage(params),
                new QueryWrapper<CategoryBrandRelationEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void saveDetail(CategoryBrandRelationEntity categoryBrandRelation) {
        //新增分类品牌关联表
        //查找分类数据
        CategoryEntity categoryEntity = categoryService.getById(categoryBrandRelation.getCatelogId());
        //查找品牌数据
        BrandEntity brand = brandService.getById(categoryBrandRelation.getBrandId());
        //给关联表的 品牌&分类 name字段 赋值
        categoryBrandRelation.setBrandName(brand.getName());
        categoryBrandRelation.setCatelogName(categoryEntity.getName());
        //保存关联数据
        this.save(categoryBrandRelation);
    }

    @Override
    public void updateCascadeBrand(Long brandId, String name) {
        LambdaUpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new LambdaUpdateWrapper<>();
        //update category_brand_relation set brandName = name where brandId = brandId
        updateWrapper.set(CategoryBrandRelationEntity::getBrandName,name).eq(CategoryBrandRelationEntity::getBrandId,brandId);
        this.update(updateWrapper);
    }

    @Override
    public void updateCascadeCategory(Long catId, String name) {
        LambdaUpdateWrapper<CategoryBrandRelationEntity> updateWrapper = new LambdaUpdateWrapper<>();
        //update category_brand_relation set brandName = name where brandId = brandId
        updateWrapper.set(CategoryBrandRelationEntity::getCatelogName,name).eq(CategoryBrandRelationEntity::getCatelogId,catId);
        this.update(updateWrapper);
    }

    @Override
    public List<BrandEntity> categoryBrandRelationList(Long catId) {
        //根据分了Id 查找 品牌分类关联表查找到所有品牌
        List<CategoryBrandRelationEntity> catBrandRelations = catBrandRelation.list(new LambdaQueryWrapper<CategoryBrandRelationEntity>().eq(CategoryBrandRelationEntity::getCatelogId, catId));

        if(CollectionUtils.isEmpty(catBrandRelations)){
            log.info("根据分类Id没有查找品牌分类关联表");
            return null;
        }

        List<Long> brandIds = catBrandRelations.stream().map(item -> {
            return item.getBrandId();
        }).collect(Collectors.toList());

        //根据品牌ID 集合查询品牌相关数据信息。
        return (List<BrandEntity>) brandService.listByIds(brandIds);
    }

}